<?php
    include_once("../action/checkAuthenticationAction.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<title>Quản lý khuyến mãi</title>
	<?php 
        include("import-css.php");
        include("../util/constant.php");
        include("../util/message.php");
    ?>
    
</head>
<body>
	<?php include("header.php");?>
<form id="form" method="post">
	<div class="container-fluid">
		<div class="row-fluid">
			<?php 
                include("menu.php");
                include_once("../service/productService.php");
            ?>
			<div id="content" class="span10">
            	<?php
					$class = null;
                    $message = null;
					
					 if(isset($_GET["action"])){
						 $action= $_GET["action"];
						 
						 if($action == DELETE_SUCCESSFULLY){
                            $message = $messages["delteteDiscoutSuccessfully"];
                            $class = SUCCESSFULL_MESSAGE_CLASS;
                        } else if($action == DELETE_FAILED){
                            $message = $messages["delteteProductStatusFailed"];
                            $class = FAILED_MESSAGE_CLASS;
                        }
						
						if(isset($class)){
				?>
                			<div id="msg" class="center alert <?php showClass($class);?>"><?php showMessage($message);?></div>
                <?php
						}
                    }
                ?>
                <div class="row-fluid sortable">
    				<div class="box span12">
    					<div class="box-content">
                            
                            <table class="table table-striped table-bordered bootstrap-datatable datatable">
                                <thead>
                                    <tr>
                                        <th>STT</th>
                                        <th>Tên</th>
                                        <th>Giá trị</th>
                                        <th>Chức năng</th>
                                    </tr>
                                 </thead>   
                                 <tbody>
                                    <?php
                                        $discounts = getAllDiscounts();
                                        $count = 1;
                                        while($row = ConnectionManager::getInstance()->fetch($discounts)) {
                                    ?>
                                            <tr> 
                                                <td><?php echo $count++;?></td>
                                                <td><?php echo $row["DIS_NAME"];?></td>
                                                <td><?php echo $row["DIS_VALUE"] * 100;?></td>                                                <td>
                                                	<?php
														$require_perm = array("edit promotion");
														if(authUtil($require_perm,$perm_array,false)){
													?>
	                                                    <a class="btn btn-info" href="discount-detail.php?action=<?php echo EDIT_ACTION; ?>&id=<?php echo $row["DIS_ID"];?>">
                                                        <i class="icon-edit icon-white"></i>Cập nhật
                                                    	</a>
                                                    <?php
														}
													?>
                                                    
                                                    <?php
														$require_perm = array("delete promotion");
														if(authUtil($require_perm,$perm_array,false)){
													?>
														<?php
                                                            if(!isDeletableDiscount($row["DIS_ID"])){
                                                        ?>
                                                            <a class="btn btn-danger" href="#" onclick="if(confirmDelete()) deleteDiscount(<?php  echo $row["DIS_ID"];?>);">
                                                                <i class="icon-trash icon-white"></i> 
                                                                Xóa
                                                            </a>
                                                        <?php
                                                            }	
                                                        ?>
                                                    <?php
														}
                                                    ?>
                                                </td>
                                            </tr>
                                    <?php
                                        }
                                    ?>
                                 </tbody>
                             </table>    
    					</div>
    				</div><!--/span-->
    			</div><!--/row-->
            </div><!-- end of content -->
        </div><!-- end of row-fluid -->
        <?php include("footer.php");?>
    </div><!-- container-fluid -->
    <?php include("import-js.php");?>
    <script>
		function deleteDiscount(disId){
			submitForm('form', '../action/deleteDiscountAction.php?disId=' + disId, 'post');
		}
	</script>
</form>
</body>
</html>